$('.header').load('head.html');
$('.footer').load('foot.html');
$('.s-car').load('side.html');




var sAdd = document.querySelector('.s-add img')
var sInp = document.querySelector('.s-sum').innerHTML - 0
var sSub = document.querySelector('.s-sub img')
var sVal = document.querySelector('.s-sum')

//add
sAdd.addEventListener('click',function(){
    sInp=sInp+1
    // sInp.innerHTML=sInp
    sVal.innerHTML=sInp
    console.log(sInp)

})

sSub.addEventListener('click',function(){
   
    if(sInp>0){
         sInp=sInp-1
          sVal.innerHTML=sInp
    }else{
        sVal.innerHTML=0
    }
    console.log(sInp)
})
// var sSec2=document.querySelector('.s-proleft>.s-sec2')
// console.log(sSec2)
// var  sN=parseInt(Math.random() * 10000)
// var sYzm=document.querySelector('.s-yzm')
// sYzm.innerHTML=sN


var codeStr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var sYzm=document.querySelector('.s-yzm')

    // 用来生成随机整数
    function getRandom(n, m) { // param: (Number, Number)
        n = Number(n);
        m = Number(m);
        // 确保 m 始终大于 n
        if (n > m) {
            var temp = n;
            n = m;
            m = temp;
        }
        return Math.floor(Math.random()*(m - n) + n);
    }
    // 将随机生成的整数下标对应的字母放入div中
    function getCode() {
        var str = '';
        // 验证码有几位就循环几次
        for (var i = 0;i < 4;i ++) {
            var ran = getRandom(0, 62);
            str += codeStr.charAt(ran);
        }
        sYzm.innerHTML = str;
    }
    getCode();// 调用a函数，页面刷新也会刷新验证码
    // 点击刷新验证码
    sYzm.onclick = function(){
        getCode();
    }
    var sShop = document.querySelector('.s-shop')
    // console.log(sShop)
    sShop.addEventListener('click', function (ev) {
        // var sN=ev.target.previousElementSibling.getAttribute('id')

        var sN = ev.target.previousElementSibling.previousElementSibling.children[0].getAttribute('id')
        console.log(sN)
        var sImg = ev.target.parentNode.previousElementSibling.children[0].children[0].src
        console.log(sImg)
        var sPrice = sShop.previousElementSibling.previousElementSibling.children[0].innerHTML
        console.log(sPrice)
        var sTitle = sShop.previousElementSibling.previousElementSibling.previousElementSibling.innerHTML
        console.log(sTitle)
        var sNum = parseInt(sShop.previousElementSibling.children[0].innerHTML)
        console.log(sNum)

        var shop1 = localStorage.getItem('shopArr')
        if (shop1) {
            var oldarr1 = JSON.parse(shop1)
            shopdj(oldarr1)
        } else {
            var oldarr1 = []
            shopdj(oldarr1)
        }


        var sSp2 = document.querySelector('.s-p2')

        sSp2.innerHTML = `购物车中共有 <span> 1 </span>种商品，合计：  <span class="ppp">￥${json.price}</span>`
        var sSto = document.querySelector('.s-coushu')
        sSto.innerHTML = `${json.price}`

        function shopdj(arr) {
            function findArr(arr, sN) {
                for (let i = 0; i < arr.length; i++) {
                    // 有相同去++
                    if (arr[i].id === sN) {
                        iNow = i
                        return true
                    }
                }
                return false
            }
            let flag = findArr(arr, sN)
            if (flag) {
                // count++
                arr[iNow].count += sNum
            } else {
                arr.push({
                    id: sN,
                    img: sImg,
                    price: sPrice,
                    title: sTitle,
                    count: sNum,
                    flag: false
                })
            }
            localStorage.setItem('shopArr', JSON.stringify(arr))
            console.log(arr)
        }

    })

// console.log(sN)
